<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class NoticeController extends Controller
{
    use ModelForm;
    public $header = '通知';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑'.$this->header);
            $content->description('通知');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * 创建组织
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建'.$this->header);

            $content->body($this->form());

        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {
            $grid->model()->where('type', 2);
            $grid->id('ID')->sortable();
            $grid->title('标题');

            $status = [
                'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
            ];
            $grid->status('状态')->switch($status);
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Article::class, function (Form $form) {
            $form->tab('通知发布', function ($form) {

                $form->display('id', '通知ID');
                $form->radio('type','广告位')->options([
                    2 => '站内通知',
                ])->default(2);
                $form->text('title','标题')->rules('required');
                $form->editor('content','详情')->rules('required');
                $form->number('sort','排序(序号小的在前)')->default(100);
                $form->switch('status', '状态')->states([
                    'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
                    'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
                ])->default(1);
            });

        });
    }
}
